package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 权限管理
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;
    /**
     * 分页
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Permission> page = permissionDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询数据库并回显
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    /**
     * 根据Id更新数据库
     * @param permission
     */
    @Override
    @Transactional
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    /**
     * 增加权限
     * @param permission
     */
    @Override
    @Transactional
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    /**
     * 根据id删除权限
     * @param id
     * @return
     */
    @Transactional
    @Override
    public void deleteById(Integer id) {
        Long count = permissionDao.findCountById(id);
        if (count<=0){
            permissionDao.deleteById(id);
        }else {
            throw new RuntimeException("无法删除");
        }
    }
}
